<?php
 
/*
 * Following code will list all the products
 */
 
// array for JSON response
$response = array();
 
 // check for required fields
if (isset($_GET['place_id'])){
	$place_id = $_GET['place_id'];
 
	// include db connect class
	require_once __DIR__ . '/db_connect.php';
	 
	// connecting to db
	$db = new DB_CONNECT();
	 
	// get all products from products table
	$result = mysql_query("SELECT 
								d.name as district_name, 
								pr.name as province_name,
								su.utility_name,
								ss.space_place_name,
								sg.group_customer_name,
								pl.* 
							FROM place pl
								LEFT JOIN district d
								ON pl.district_id = d.id
								LEFT JOIN province pr
								ON pr.id = d.province_id
								LEFT JOIN 
								(
									SELECT d.place_id, GROUP_CONCAT( u.name SEPARATOR  '; ' ) as utility_name
									FROM detail_utility_place d
										INNER JOIN utility u
										ON d.utility_id = u.id
									GROUP BY d.place_id
								) su
								ON pl.place_id = su.place_id
								LEFT JOIN 
								(
									SELECT d.place_id, GROUP_CONCAT( u.name SEPARATOR  '; ' ) as space_place_name
									FROM detail_space_place d
										INNER JOIN space_place u
										ON d.space_place_id = u.id
									GROUP BY d.place_id
								) ss
								ON pl.place_id = ss.place_id
								LEFT JOIN 
								(
									SELECT d.place_id, GROUP_CONCAT( u.name SEPARATOR  '; ' ) as group_customer_name
									FROM detail_group_customer_place d
										INNER JOIN group_customer u
										ON d.group_customer_id = u.id
									GROUP BY d.place_id
								) sg
								ON pl.place_id = sg.place_id
							WHERE pl.place_id = '$place_id'") or die(mysql_error());
	 
	// check for empty result
	if (mysql_num_rows($result) > 0) {
		// looping through all results
		// products node
		$response["place"] = array();
	 
		while ($row = mysql_fetch_array($result)) {
			// temp user array
			$place = array();    
		$place["utility_name"] = $row['utility_name'];
		$place["space_place_name"] = $row['space_place_name'];
		$place["group_customer_name"] = $row['group_customer_name'];
		$place["place_id"] = $row['place_id'];
		$place["place_name"] = $row['place_name'];
		$place["address"] = $row['address'];
		$place["province_name"] = $row['province_name'];	
		$place["district_name"] = $row['district_name'];	
		$place["district_id"] = $row['district_id'];	
		$place["longitude"] = $row['longitude'];
		$place["latitude"] = $row['latitude'];	 
		$place["phone_number"] = $row['phone_number'];
		$place["fax_number"] = $row['fax_number'];
		$place["email"] = $row['email'];
		$place["website"] = $row['website'];
		$place["open_time"] = $row['open_time'];		 
		$place["close_time"] = $row['close_time'];
		$place["lowest_price"] = $row['lowest_price'];
		$place["hightest_price"] = $row['hightest_price'];
		$place["description"] = $row['description'];
		$place["url_avatar"] = $row['url_avatar'];	
		$place["name_avatar"] = $row['name_avatar'];	
		$place["date_create"] = $row['date_create'];
		$place["public"] = $row['public'];
		$place["delete"] = $row['delete'];
		
			// push single product into final response array
			array_push($response["place"], $place);
		}
		// success
		$response["success"] = 1;
	 
		// echoing JSON response
		echo json_encode($response["place"]);
	} else {
		// no products found
		$response["success"] = 0;
		$response["message"] = "No place found";
	 
		// echo no users JSON
		echo json_encode($response);
	}
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>